#include "los_task.h"
#include "stdio.h"

UINT32 g_taskHiId = 0;
UINT32 g_taskLoId = 0;

INT32 hiPrintCount = 0;
INT32 loPrintCount = 0;

BOOL hiTaskAllowed = TRUE;

UINT32 TaskHiEntry(VOID)
{
    printf("高优先级任务启动\n");
    
    while (1) {
        if (hiTaskAllowed) {
            printf("学号: 2023192034\n"); 
            hiPrintCount++;
            
            if (hiPrintCount >= 2) {
                hiTaskAllowed = FALSE; 
                printf("高优先级任务已被禁止\n");
            }
        }
        
        LOS_TaskDelay(100); 
    }
    
    return LOS_OK;
}

UINT32 TaskLoEntry(VOID)
{
    printf("低优先级任务启动\n");
    
    while (1) {
        printf("姓名: 邹紫曼\n"); 
        loPrintCount++;
        
        if (loPrintCount >= 3) {
            printf("低优先级任务已完成3次打印,即将删除自己\n");
            
            UINT32 ret = LOS_TaskDelete(g_taskLoId);
            if (ret != LOS_OK) {
                printf("删除低优先级任务失败! 错误码: %d\n", ret);
            } else {
                printf("低优先级任务已成功删除\n");
            }
            
            break; 
        }
        
        LOS_TaskDelay(200); 
    }
    
    return LOS_OK;
}

UINT32 Example_TaskCaseEntry(VOID)
{
    UINT32 ret;
    TSK_INIT_PARAM_S initParam;
    
    LOS_TaskLock();
    printf("任务调度已锁定\n");
    
    initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskHiEntry;
    initParam.usTaskPrio = 10;  
    initParam.pcName = "HiTask";
    initParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    initParam.uwResved = 0;
    
    ret = LOS_TaskCreate(&g_taskHiId, &initParam);
    if (ret != LOS_OK) {
        LOS_TaskUnlock();
        printf("高优先级任务创建失败! 错误码: %d\n", ret);
        return LOS_NOK;
    }
    printf("高优先级任务创建成功, ID: %d\n", g_taskHiId);
    
    initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskLoEntry;
    initParam.usTaskPrio = 20; 
    initParam.pcName = "LoTask";
    
    ret = LOS_TaskCreate(&g_taskLoId, &initParam);
    if (ret != LOS_OK) {
        LOS_TaskUnlock();
        printf("低优先级任务创建失败! 错误码: %d\n", ret);
        return LOS_NOK;
    }
    printf("低优先级任务创建成功, ID: %d\n", g_taskLoId);
    
    LOS_TaskUnlock();
    printf("任务调度已解锁\n");
    
    return LOS_OK;
}

int main(void)
{
    printf("任务管理示例开始\n");
    
    if (Example_TaskCaseEntry() != LOS_OK) {
        printf("任务创建失败!\n");
        return -1;
    }
    
    while (1) {
        LOS_TaskDelay(1000);
    }
    
    return 0;
}